//
//  HomeOneGroup.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit

class HomeOneGroup: NSObject {
    var item = [HomeItemModel]()
    var title : String?
    var rectangle5_image = ""
    var rectangle5_data = ""
    var rectangle2_image = ""
    var rectangle1_data = ""
    var rectangle2_type = ""
    var rectangle3_data = ""
    var rectangle4_type = ""
    var rectangle4_image = ""
    var rectangle1_image = ""
    var rectangle4_data = ""
    var rectangle2_data = ""
    var rectangle1_type = ""
    var rectangle3_image = ""
    var rectangle3_type = ""
    var rectangle5_type = ""
    var type = ""
    var data = ""
    var image = ""
    
    lazy var likeItems : [[HomeItemModel]] = {
        
        var arrayM = [[HomeItemModel]]()
        var subArrayM : [HomeItemModel]?
        
        for (index,value) in self.item.enumerated() {
            if index % 2 == 0 {
                
                subArrayM = [HomeItemModel]()
                
                subArrayM?.append(value)
                
                
            }else{
                
                subArrayM?.append(value)
                
                arrayM.append(subArrayM!)
                
                
            }
            
            
        }
        
        
        if self.item.count % 2 == 1 {
            
            subArrayM = [HomeItemModel]()
            
            subArrayM?.append(self.item.last!)
            
            arrayM.append(subArrayM!)
            
        }
        
        
        
        return arrayM
        
        
    }()
    
    
    init(dic : [String : AnyObject]) {
        super.init()
        
        setValuesForKeys(dic)
        
    }
    
    override func  setValue(_ value: Any?, forKey key: String) {
    
        
        if key == "item" {
            
            if let temp = value as? [[String : AnyObject]]{
                
                for dic in temp {
                    
                    item.append(HomeItemModel(dic:dic ))
                }
                
                
            }
            
        } else {
            super.setValue(value, forKey: key)
        }
        
        

    
    
    }
    
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        
        }
    
    
    //在swift中想要打印对象要以下重写属性
    override var description: String{
        let keys = ["item","title","rectangle5_image","rectangle5_data","rectangle5_type","type","data","image","rectangle4_image","rectangle4_data","rectangle4_type","rectangle3_image","rectangle3_data","rectangle3_type","rectangle2_image","rectangle2_data","rectangle2_type","rectangle1_image","rectangle1_data","rectangle1_type"]
        let dic = dictionaryWithValues(forKeys: keys)
        return "\(dic)"
    }
    

    
}
